﻿/// Author:					Le Xuan Manh
/// Created:				2011-10-06
/// Last Modified:			2011-10-06

using System;
using System.Configuration.Provider;
using System.Web.Configuration;
using log4net;

namespace ITC.Business.WebHelpers.UserRegisteredHandlers
{
    /// <summary>
    ///  
    /// </summary>
    public sealed class UserRegisteredHandlerProviderManager
    {
        private static object initializationLock = new object();
        private static readonly ILog log
            = LogManager.GetLogger(typeof(UserRegisteredHandlerProviderManager));

        static UserRegisteredHandlerProviderManager()
        {
            Initialize();
        }

        private static void Initialize()
        {
            providerCollection = new UserRegisteredHandlerProviderCollection();

            try
            {
                UserRegisteredHandlerProviderConfig config
                    = UserRegisteredHandlerProviderConfig.GetConfig();

                if (config != null)
                {

                    if (
                        (config.Providers == null)
                        || (config.Providers.Count < 1)
                        )
                    {
                        throw new ProviderException("No UserRegisteredHandlerProviderCollection found.");
                    }

                    ProvidersHelper.InstantiateProviders(
                        config.Providers,
                        providerCollection,
                        typeof(UserRegisteredHandlerProvider));

                }
                else
                {
                    // config was null, not a good thing
                    log.Error("UserRegisteredHandlerProviderConfig could not be loaded so empty provider collection was returned");

                }
            }
            catch (NullReferenceException ex)
            {
                log.Error(ex);
            }
            catch (TypeInitializationException ex)
            {
                log.Error(ex);
            }
            catch (ProviderException ex)
            {
                log.Error(ex);
            }
            
            providerCollection.SetReadOnly();
                
            
        }


        private static UserRegisteredHandlerProviderCollection providerCollection;

        public static UserRegisteredHandlerProviderCollection Providers
        {
            get
            {
                if (providerCollection == null) Initialize();
                return providerCollection;
                
            }
        }

    }
}
